// 随机鼓励语句库
const motivations = [
    { 
        text: "你的心理韧性比上周提升了{{percent}}% 🌱 每一次坚持都在塑造更好的自己！",
        icon: '🌱'
    },
    {
        text: "本周有{{days}}天指数超过80分 🌟 继续保持这种积极状态吧！",
        icon: '🌟'
    },
    {
        text: "最近3天指数稳定上升 📈 说明你的自我调节正在生效！",
        icon: '📈'
    }
];

// 图表数据
const chartData = {
    labels: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'],
    datasets: [{
        label: '心理状态',
        data: [78, 85, 72, 89, 82, 76, 98],
        borderColor: '#7dd3fc',
        backgroundColor: 'rgba(125, 211, 252, 0.1)',
        tension: 0.3,
        fill: true,
        pointRadius: 6,
        pointBackgroundColor: '#7dd3fc',
        pointBorderColor: '#ffffff',
        pointHoverRadius: 8,
        borderWidth: 2
    }]
};

// 图表配置
const config = {
    type: 'line',
    data: chartData,
    options: {
        responsive: true,
        maintainAspectRatio: false,
        scales: {
            y: {
                min: 0,
                max: 100,
                grid: { color: '#e2e8f0' },
                title: { 
                    display: true,
                    text: '健康指数',
                    color: '#64748b'
                },
                ticks: { color: '#94a3b8' }
            },
            x: {
                grid: { display: false },
                ticks: { color: '#94a3b8' }
            }
        },
        plugins: {
            legend: {
                labels: { color: '#475569' },
                position: 'top',
            },
            tooltip: {
                mode: 'index',
                intersect: false,
                backgroundColor: '#ffffff',
                titleColor: '#334155',
                bodyColor: '#475569',
                borderColor: '#e2e8f0',
                borderWidth: 1,
                boxShadow: '0 2px 8px rgba(0,0,0,0.1)'
            }
        }
    }
};

// 页面加载初始化
document.addEventListener('DOMContentLoaded', function() {
    // 随机选择鼓励语
    const randomMotivation = motivations[Math.floor(Math.random() * motivations.length)];
    const encouragementText = document.getElementById('encouragementText');

    // 动态数据计算
    const data = chartData.datasets[0].data;
    const dataStats = {
        percent: ((data[data.length-1] - data[0]) / data[0] * 100).toFixed(0),
        days: data.filter(score => score >= 80).length
    };

    // 替换模板变量
    let finalText = randomMotivation.text
        .replace('{{percent}}', Math.abs(dataStats.percent))
        .replace('{{days}}', dataStats.days);

    encouragementText.innerHTML = `${finalText} ${randomMotivation.icon}`;

    // 初始化图表
    const ctx = document.getElementById('moodChart').getContext('2d');
    new Chart(ctx, config);


});